
//LiteXLoader Dev Helper
/// <reference path="c:\Users\stranger\.vscode\extensions\moxicat.lxldevhelper-0.1.4/Library/JS/Api.js" /> 


//这里包含了服务器一些基本的配置项 和对一些插件的修复

logger.setConsole(true);
mc.listen("onServerStarted",function(){//打印banner
    mc.setMotd("§4Half§6Island §9welcome§5!!!")//Motd
    logger.info("[HalfIsland]>>>>>>>>>>>>>>>> finished... HalfIsland is loaded!");//加载输出
})

mc.listen("onPlayerDie",function(player,source){//死亡一次扣随机等级 随机金钱 

    let Level=Math.floor(Math.random()*30);

    if(player){//玩家对象非空
        mc.broadcast( `§6[提示] §9${player.name}死亡，降低随机等级，损失随机金钱`);
        mc.runcmdEx(`money reduce ${player.name} 2021`);
        player.addLevel(-Level);
    }

    let playerList=mc.getOnlinePlayers();
    let randomIndx=Math.floor(Math.random()*playerList.length);
    let randomPlayer=playerList[randomIndx];
    
    if(randomPlayer){//随机玩家获得死亡奖励
        mc.broadcast(`§6[提示] §9玩家${randomPlayer.name} 获得了玩家${player.name}的死亡奖励`);
        mc.runcmdEx(`money add ${randomPlayer.name} 2021`);
        randomPlayer.addLevel(Level);
    }

})

mc.listen("onUseItemOn",function(player,item,block,side){//家园 公会 lore等菜单异常修复
    if(player&&item){
        if(!player.hasTag("north")){
            selectForWin10(player);
            if(item.name==="§9指§5北§e针"){
                let fm=menu();
                player.sendForm(fm,function(player,id){
                    if(player&&id>=0&&id<=3){
                        let cmd="";
                        switch(id){
                            case 0:{
                                cmd="home";
                            }break;
                            case 1:{
                                cmd="guild";
                            }break;
                            case 2:{
                                cmd="lore";
                            }break;
                            case 3:{
                                 titleAddress(player);   
                            }
                        }
                        if(cmd!=="")
                            player.runcmd(cmd);
                    }
                });
            }
        }
    }
})

mc.listen("onLeft",function(player){
    if(player){
        player.removeTag("north");
    }
})
mc.listen("onJoin",function(player){
    if(player){
        titleAddress(player);
    }
})
mc.listen("onDestroyBlock",function(player,block){//挖矿加强
    if(player&&block){
        let hand=player.getHand();
        let itName=hand.name;

        if(itName==="§4传奇§6锯刃十字镐"){

            let dir=player.direction;
            let pos=block.pos;//坐标对象
            let dimid=pos.dimid;//维度

            let blx=pos.x;//被破坏方块的坐标
            let bly=pos.y;
            let blz=pos.z;

            let pitch=dir.pitch;//仰俯角 -90 —— 90

            let blx1=(blx-1);
            let bly1=(bly);
            let blz1=(blz-1);
            

            let blx2=(blx+1);
            let bly2=(bly+(pitch<=60?2:-2));//计算角度判断是向下还是向上，
            let blz2=(blz+1);
                       
            mc.runcmdEx(`execute ${player.name} ~ ~ ~/fill ${blx1.toString()} ${bly1.toString()} ${blz1.toString()} ${blx2.toString()} ${bly2.toString()} ${blz2.toString()} air 0 destroy`);
                 
        }
    }
})


function titleAddress(player){

    let pos=player.pos;

    if(pos.dimid===0)
    {
        let coordinate=toLongitudeAndLatitude(pos.x,pos.z);
        let lot=coordinate.lot.toString();
        let lat=coordinate.lat.toString();

        getAddress(lot,lat,player);//获取地理位置信息
    }
    else{
        player.tell("§6[高德地图] §3高德地图仅支持主世界定位服务");
    }
}

function getAddress(lot,lat,player){//获取地理位置信息

    let url=`https://restapi.amap.com/v3/geocode/regeo?key=0f92ce0c5e329928197f4058ded2d5d3&location=${lot},${lat}`;

    network.httpGet(url,function(status,result){

        let res=JSON.parse(result);
        let address=res.regeocode.addressComponent;//地址信息

        let cities=address.city;
        let prov=address.province;
        let country=address.country;
        let district=address.district;
        let msg="";


        if(country.toString()==="")
            msg=`§6[高德地图] §3当前地理位置为:国外 纬度${lot} 经度${lat}`;
        else{
             msg=`§6[高德地图] §3当前地理位置为:${country}-${prov}-${cities}${district}`;
        }    
        player.tell(msg);
    })
}
//转换成真实的经纬度
function toLongitudeAndLatitude(x,y){

    let coordinate={
        lot:0,
        lat:0,
    }

    if(!x&&!y)
        return coordinate;
    
    y=-y;// y坐标是反的

    coordinate=compute(x,y);
        
    return  coordinate;
}

function compute(x,y){
    return {lot:x/60.00000,lat:y/60.00000};
}

function menu(){
    let fm=mc.newSimpleForm();

    fm=fm.setTitle("指北针菜单");
    fm=fm.addButton("家园传送");
    fm=fm.addButton("公会系统");
    fm=fm.addButton("物品备注");
    fm=fm.addButton("查询地理位置")
    return fm;
}

function selectForWin10(player){//避免win10玩家出现的多次触发

    player.addTag("north");//加标签

    setInterval(()=>{//异步删除标签
        player.removeTag("north");
    },350);
}

function title(data,player){
    mc.runcmdEx(`title ${player.name} actionbar §6${data}`);
}

const banner=`
    _   _       _  __ _____    _                 _  ______               _             
   | | | |     | |/ _|_   _|  | |               | | |  _  \\             | |            
   | |_| | __ _| | |_  | | ___| | __ _ _ __   __| | | | | |_____   _____| | ___  _ __  
   |  _  |/ _\` | |  _| | |/ __| |/ _\` | '_ \\ / _\` | | | | / _ \\ \\ / / _ \\ |/ _ \\| '_ \\ 
   | | | | (_| | | |  _| |\\__ \\ | (_| | | | | (_| | | |/ /  __/\\ V /  __/ | (_) | |_) |
   \\_| |_/\\__,_|_|_|  \\___/___/_|\\__,_|_| |_|\\__,_| |___/ \\___| \\_/ \\___|_|\\___/| .__/ 
                                                                                | |    
                                                                                |_|    `
log(banner);
logger.info("[HalfIsland]version:1.0.1")
logger.info("[HalfIsland]Author:Stranger")